<template>
  <div class="login-form">
    <div class="form">
      <div style="font-weight: bolder">欢迎使用宾馆管理系统</div>
      <el-form :model="form">
        <el-form-item label="账号">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="sendLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {login} from "@/api/AdminApi";
import Cookies from 'js-cookie'

export default {
  name: "LoginForm",
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    sendLogin() {
      login(this.form.username, this.form.password).then(res => {
        this.$store.commit('setToken', res['data']['token'])
        this.$store.commit('setTokenHead', res['data']['tokenHead'])
        Cookies.set('token', res['data']['token'])
        Cookies.set('tokenHead', res['data']['tokenHead'])
        this.$router.push('/')
      }).catch(e => {
        this.$message.error(e.message)
      })
    }
  }
}
</script>

<style scoped>
.login-form {
  text-align: center;
}

.form {
  width: 300px;
  margin: auto;
}
</style>
